package cn.lxb.emall.gw.filter;

import java.util.function.Function;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory;
import org.springframework.web.server.ServerWebExchange;

import lombok.Getter;
import lombok.Setter;

/** 可根据条件修改请求体的过滤器工厂 */
public class ConditionalModifyRequestBodyGatewayFilterFactory extends ModifyRequestBodyGatewayFilterFactory {

	@Setter
	@Getter
	Function<ServerWebExchange, Boolean> condition;

	@Override
	public GatewayFilter apply(Config config) {
		GatewayFilter gf = super.apply(config);
		return (exchange, chain) -> {
			if (Boolean.TRUE.equals(condition.apply(exchange))) {
				return gf.filter(exchange, chain);
			} else {
				return chain.filter(exchange);
			}
		};
	}

}
